package com.attilax.office;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.attilax.ex.ExUtil;
import com.attilax.log.LogUtil;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComFailException;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import word.WordServer;

public class Com_jacob_office_Util {

	public static void main(String[] args) {
	//	LogUtil.redirectSystemOutAndErrToLog_slf4j();
		String path = "D:\\atitit doc文档类索引与目录v2encry.docx";
		path="d:\\222-20170321.docx";
		path="D:\\Atitit php与java计算页数后端.zip.docx";
	 //	path = "D:\\atitit doc文档类索引与目录v2.docx";
	//	int p = new Com_jacob_office_Util().getDoc_page(path);
		
		int p = new Com_jacob_office_Util().getpptPage("d:\\a.pptx");
		System.out.println("page:"+p);
	}

	static WordServer instance;
	public ActiveXComponent word_ActiveXComponent;
	public ActiveXComponent powerpoint_ActiveXComponent;
	public Dispatch doc_Dispatch;
	public Dispatch ppt_Dispatch;
	int port = 8888;
	public static final Logger logger = LoggerFactory.getLogger("Com_jacob_office_Util");
	public int getDoc_page(String path) {
		logger.info( " getDoc_page() path:"+path);
		if(!new File(path).exists())
			throw new RuntimeException(" file not exist:"+path);
		System.out.println("");

		// ini
		word_ActiveXComponent = new ActiveXComponent("Word.Application");
		System.out.println(JSON.toJSONString(word_ActiveXComponent, true));
		word_ActiveXComponent.setProperty("Visible", new Variant(true)); // 不可见打开word
		word_ActiveXComponent.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏

		// Documents表示word的所有文档窗口（word是多文档应用程序）
		// 调用wrdCom.Documents.Open方法打开指定的word文档，返回wordDoc
		// int pages =
		// Integer.parseInt(Dispatch.call(selection,"information",4).toString());//总页数
		// //显示修订内容的最终状态
		// //获得documents对象 get doc obj. //word所有文档
		doc_Dispatch = word_ActiveXComponent.getProperty("Documents").toDispatch();

		// open one docu...
		boolean ConfirmConversions = false;
		boolean ReadOnly = false;
		boolean AddToRecentFiles = false;
		String PasswordDocument = "aa";
 		try {
			Dispatch.call(doc_Dispatch, "Open", path, ConfirmConversions, ReadOnly, AddToRecentFiles,
					new Variant(PasswordDocument)).toDispatch();
 		}catch(Exception e)
 		{
 			closeWord();
 			com.attilax.exception.ExUtil.throwExV2(e);
 		}
 		finally {
 			
		}
// 	} catch (ComFailException e) {
// 			if(e.getMessage().contains("密码不正确"))
// 				
// 		}
	
		// wordDoc = Dispatch.call(wrdDocs, "Open", docFilePath, false,//
		// ConfirmConversions
		// true, false, new Variant(password)).toDispatch()
		Dispatch ActiveDocument = Dispatch.get(word_ActiveXComponent, "ActiveDocument").toDispatch();
		int page = Dispatch.call(ActiveDocument, "ComputeStatistics", new Variant(2)).toInt();
		closeWord();

		// 关闭com的线程
		// ComThread.Release();
	     
		return page;

	}

	private void closeWord() {
		
		
//	 try {
//		 word_ActiveXComponent.safeRelease();
//	} catch (Exception e) {
//		 e.printStackTrace();
//	}
		
		try {
			 if( doc_Dispatch!=null)
			Dispatch.call(doc_Dispatch, "Close", new Variant(false));
		} catch (Exception ex2) {
			System.out.println("------------------------wanring  Dispatch.call(doc_Dispatch, Close");
			ex2.printStackTrace();
			System.out.println("--------------------wanringend  Dispatch.call(doc_Dispatch, Close");
		}

		try {
			word_ActiveXComponent.invoke("Quit", new Variant[0]);
			word_ActiveXComponent.invoke("Quit", new Variant[] {});
		} catch (Exception e) {
			logger.error(" word_ActiveXComponent.invoke(Quit  ",e);
		//	e.printStackTrace();
			//ExUtil.throwExV2(e);
		}
		
		try {
			System.out.println("ComThread  clos");
			  //关闭com的线程   
		       ComThread.Release();   
		} catch (Exception e) {
			logger.error( "ComThread.Release()", e);
		}
	}

	public int getpptPage(String path) {

		if (!new File(path).exists())
			throw new RuntimeException(" file not exist:" + path);

		powerpoint_ActiveXComponent = new ActiveXComponent("Powerpoint.Application");
		powerpoint_ActiveXComponent.setProperty("Visible", new Variant(true)); // 不可见打开ppt
		// powerpoint.setProperty("AutomationSecurity", new Variant(3)); //禁用宏
		ppt_Dispatch = powerpoint_ActiveXComponent.getProperty("Presentations").toDispatch();

		int page = 0;
		Dispatch activePresentation_Dispatch = null;

		try {
			Dispatch.call(ppt_Dispatch, "Open", path).toDispatch();
			activePresentation_Dispatch = powerpoint_ActiveXComponent.getProperty("ActivePresentation").toDispatch();
			Dispatch slides = Dispatch.call(activePresentation_Dispatch, "Slides").getDispatch();
			page = Dispatch.call(slides, "Count").toInt();
			 
		} catch (Exception e) {
			com.attilax.exception.ExUtil.throwExV2(e);
		     
		}finally{
			pptClose(activePresentation_Dispatch);

		}

	
		return page;
		// return port;
	}

	private void pptClose(Dispatch activePresentation_Dispatch) {
		
		
	
		
		
		try {
			if (activePresentation_Dispatch != null)
				Dispatch.call(activePresentation_Dispatch, "Close");

			// ComThread.Release(); ati

		} catch (Exception ex2) {
			ex2.printStackTrace();
		}
		
		try {
			 if( ppt_Dispatch!=null)
			Dispatch.call(ppt_Dispatch, "Close", new Variant(false));
		} catch (Exception ex2) {
			System.out.println("--warning");
			  ex2.printStackTrace();
		}
		try {
			powerpoint_ActiveXComponent.invoke("Quit", new Variant[] {});
		} catch (Exception e) {
			logger.error(" word_ActiveXComponent.invoke(Quit  ",e);
		}
	}
}
